package com.star.common

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.star.commonlib.utils.timer.StarCountTimer
import com.star.commonlib.utils.timer.TimerChanged
import com.star.commonlib.utils.timer.startTimer
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity(), View.OnClickListener {
    private lateinit var starCountTimer: StarCountTimer
    private var upTimer: Timer? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //1
        tv_verification.text = "开始计时"
        starCountTimer = StarCountTimer(tv_verification, "重新开始", 60000, 1000)
        tv_verification.setOnClickListener(this)
        //2
        btn_start.setOnClickListener(this)
        btn_end.setOnClickListener(this)


        tv_main_pop.setOnClickListener(this)
        tv_main_dialog.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_main_pop -> {
                PopupWindowActivity.start(this)
            }
            R.id.tv_main_dialog -> {
                DialogActivity.start(this)
            }

            //倒计时
            R.id.tv_verification -> {
                starCountTimer.start()
            }
            //正向计时
            R.id.btn_start -> {
                upTimer?.cancel()
                var count = 0
                upTimer = startTimer(1000, 1000, object : TimerChanged {
                    override fun onTimerChanged() {
                        runOnUiThread {
                            count++
                            tv_timer_test.text = count.toString()
                        }
                    }
                })
            }
            //结束正向计时
            R.id.btn_end -> {
                upTimer?.cancel()
            }

        }
    }

    override fun onDestroy() {
        super.onDestroy()
        upTimer?.cancel()
    }
}